//
//  QRCodeViewController.swift
//  Weibo007
//
//  Created by 闫崇 on 15/12/19.
//  Copyright © 2015年 闫崇. All rights reserved.
//

import UIKit
import AVFoundation

class QRCodeViewController: UIViewController,UITabBarDelegate,AVCaptureMetadataOutputObjectsDelegate {

    
   
    @IBOutlet weak var widthConstraint: NSLayoutConstraint!
    
    @IBOutlet weak var heightConstraint: NSLayoutConstraint!

    //顶部约束
    @IBOutlet weak var topConstraint: NSLayoutConstraint!
    
    @IBOutlet weak var scanImage: UIImageView!
    
    @IBOutlet weak var tabBar: UITabBar!
    //关闭视图控制器
    @IBAction func close(sender: AnyObject) {
        
        dismissViewControllerAnimated(true, completion: nil)
  
    }
    
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
       startAnimate()
        
        
    }
    
    
    
    //开始动画
    func startAnimate() {
        
        //添加了修改标记
        topConstraint.constant = -heightConstraint.constant
        view.layoutIfNeeded()
        
        UIView.animateWithDuration(2.0) { () -> Void in
            //重复次数需要放在内部
            UIView.setAnimationRepeatCount(MAXFLOAT)
            
            //添加了修改的标记
            self.topConstraint.constant = self.heightConstraint.constant
            //真正做动画的是这句话
            self.view.layoutIfNeeded()
        }
        
        
    }
    
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

       //默认选中第一项
        tabBar.selectedItem = tabBar.items! [0]
        
        //设置会话
        setupSession()
        
    }

 
    
    //MARK : - TabBar 代理方法
    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        print(item.tag)
        
        //改变选中的 item 的时候改变边框的大小
        heightConstraint.constant = widthConstraint.constant * (item.tag == 1 ? 0.5 : 1)
        //停止动画
        scanImage.layer.removeAllAnimations()
        
        //启动动画
        startAnimate()
    
    }
    
    
    
    //二维码扫描
    //开始扫描
    func startScan() {
        session.startRunning()
        
    }
    
    
    //设置会话
    func setupSession() {
        //1.判断能否添加设备
        if !session.canAddInput(inputDevice){
            print("无法添加输入设备")
            return
        }
        //2.判断能否添加输出数据
        if !session.canAddOutput(outputData){
            print("无法添加输出设备")
            return
        }
        print("ok")
        
        
        //3.添加设备
        session.addInput(inputDevice)
        print("前\(outputData.availableMetadataObjectTypes)")
        session.addOutput(outputData)
        //只有添加到session 后输出数据的数据类型才可用
        print("后\(outputData.availableMetadataObjectTypes)")
        
        //4.设置检测数据类型,检测所有支持的类型
        outputData.metadataObjectTypes = outputData.availableMetadataObjectTypes
        
        //5.设置数据的代理
        outputData.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
        
    }
    
    
    //MARK : - AVCaptureMetadataOutputObjectsDelegate 的代理方法
    func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
        print(metadataObjects)
    }
    
    
    
    //懒加载  session 桥梁
    lazy var session : AVCaptureSession = AVCaptureSession()
    
    //输入设备
    lazy var inputDevice : AVCaptureDeviceInput?  = {
        //取设备
       let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
        
        do {
            return try AVCaptureDeviceInput(device: device)
        }catch{
            print(error)
            return nil
        }
    }()
    
    
    
    //输出数据
    lazy var outputData : AVCaptureMetadataOutput = AVCaptureMetadataOutput()
}
